/*Printing bengali language on a 16x2 character LCD
  by-Rahul Kar*/

#include "Bengali.h"
#include "BengaliFont.h"

char inputlist[40][8];//stores the input from user
uint8_t mb[8][8];//master byte set.It stores the schematics of the custom characters to be created

void intro()//used for displaying some general information to the user
{
  Serial.println("Printing Bengali Language on a 16x2 LCD.");
  Serial.println("Please Refer To the Keyboard Layout for English To Bengali conversion.");
  Serial.println("Due To Limited RAM in Atmega 168/328 The Maximum Input Capacity Is Restricted To 40 characters.");
  Serial.println();
  Serial.println("Programmed And Designed By-Rahul Kar");
  Serial.println("Email:- yxrkt512@gmail.com");
}
void clean()//initializes the CGRAM with blanks after one set of characters is completed
{
  for(int i=0;i<8;i++)
      for(int j=0;j<8;j++)
          mb[i][j]=0x00;
}
void cleaninputlist()//initializes the input list with blanks before data entry
{
  for(int i=0;i<40;i++)
      for(int j=0;j<8;j++)
          inputlist[i][j]=0x00;
}

void charcreator(LiquidCrystal LC)//creates the custom characters in LCD ram
{
  for(int i=0;i<8;i++)
      LC.createChar(i,mb[i]);// define our characters into the sketch as variables
}

void setcreator(int len,LiquidCrystal LC)//creates custom character in sets of 8(CGRAM limitation)
{
  int i,j,c=0,z=0;
  do
  {
    clean();//clean the master byte list after displaying one set
    for(i=0;i<8;i++)
    {
        for(j=0;j<8;j++)
            mb[i][j]=inputlist[c+z][j];//taking 8 sets of data into ram
        c++;//increment the index of input list
    }
    z++;//scroll effect value
    c=0;//resets the input index for scrolling effect
    len--;//reducing the length of the input by one
    charcreator(LC);//create 8 sets of characters
    paint(LC);//call paint function for displaying in LCD
  }
  while(len>0);
}

void paint(LiquidCrystal LC)
{ 
   for(int i=0;i<8;i++)
   {
       LC.setCursor(i,0);//sets the position of cursor in LCD
       LC.write(i);//write the content of RAM at position i
       delay(300);//delay used for scroll effect
   }
}

void searchandadd(int len,int datainput[])//english to bengali font convertion
{
  int l,u,mid,y=0;
  for(int i=0;i<len;i++)
  {
    l=0,u=69;
    //using binary search for faster search
    while(l<=u)
    {
      mid=(l+u)/2;
      if(datainput[i]==engref[mid])
      {
        for(int k=0;k<8;k++)
             inputlist[y][k]=bengref[mid][k];//copy the input to inputlist from matching bengref and enfref list
        y++;
        break;
      }
      else if(datainput[i]<engref[mid])
        u=mid-1;
      else
        l=mid+1;
     }
  }
}

void Bengali_Serial(LiquidCrystal LC)//all the functions are synchronized here
{
  int datainput[40];
  int index=0;
  LC.print("Enter Data in SM");
  delay(500);
  LC.clear();
  if (Serial.available())
  {
    // wait a bit for the entire message to arrive
    delay(100);
    // read all the available characters
    while(Serial.available()>0)
    {
        datainput[index++]=Serial.read();//storing the ASCII vales in array for maping
    }
    searchandadd(index,datainput);
    setcreator(index,LC);//for processing the input list
    LC.clear();
    cleaninputlist();//cleans the input list for next print set
  }
}

void Bengali_Text(LiquidCrystal LC,String input)
{
    int index=input.length(); 
    int datainput[index];
    for(int i=0;i<index;i++)
    {
	datainput[i]=(int)input.charAt(i);
    }
    searchandadd(index,datainput);
    setcreator(index,LC);//for processing the input list
    LC.clear();
    cleaninputlist();//cleans the input list for next print set
}

void Bengali_PrintAllChar(LiquidCrystal LC,int group)
{
   int datainput[40]; 
   int index=0;
   if(group==0)
   {
      for(int i=1;i<40;i++)
      {
	   datainput[index++]=engref[i];
      }
   }
   if(group==1)
   {
      for(int i=40;i<70;i++)
      {
	   datainput[index++]=engref[i];
      }
   }
   searchandadd(index,datainput);
   setcreator(index,LC);//for processing the input list
   LC.clear();
   cleaninputlist();//cleans the input list for next print set
}
  